﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace produccion.clases
{
    public class CLSCTGREF
    {
        MDOgeneral mod = new MDOgeneral();
        string codref_crf, codprc_crf;
        double valor_crf;

        public string CODREF_CRF
        {
            get { return codref_crf; }
            set {codref_crf=value;}
        }

        public string CODPRC_CRF
        {
            get { return codprc_crf; }
            set { codprc_crf = value; }
        }

        public double VALOR_CRF
        {
            get { return valor_crf; }
            set { valor_crf=value;}
        }

        public int Insertar()
        {
            try
            {
                string query = "Insert into ctgref (codref_crf, codprc_crf, valor_crf) values ('" + CODREF_CRF + "','" + CODPRC_CRF + "'," + VALOR_CRF + ")";
                return mod.comando(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int Actualizar()
        {
            try
            {
                string query = "update ctgref set valor_crf=" + VALOR_CRF + " where codref_crf='" + CODREF_CRF + "' and codprc_crf='" + CODPRC_CRF + "'";
                return mod.comando(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable Consultar()
        {
            try
            {
                string query = "select codprc_crf, nomprc_ppr, valor_crf from ctgref inner join prcprd on codprc_crf=codprc_ppr where codref_crf='" + CODREF_CRF + "'";
                query = query + " union ";
                query = query + " select codprc_ppr, nomprc_ppr, 0 as valor_crf from prcprd where codprc_ppr not in (select codprc_crf from ctgref where codref_crf='" + CODREF_CRF + "')";
                return mod.consulta(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public Boolean Existe()
        {
            try
            {
                string query = "select codprc_crf from ctgref where codref_crf='" + CODREF_CRF + "' and codprc_crf='" + CODPRC_CRF + "'";
                if (mod.consulta(query).Rows.Count > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public CLSPRCPRD proceso = new CLSPRCPRD();
    }
}
